package com.blb.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.entity.*;
import com.blb.entity.page.BlbPage;
import com.blb.entity.page.DataTablePage;
import com.blb.service.IIoWarehouseService;
import com.blb.service.IWareService;
import com.blb.utils.ResponseHelp;
import com.blb.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author HuaHao
 * @since 2019-11-16
 */
@Controller
@RequestMapping("/ioWarehouse")
public class IoWarehouseController {

    @Resource
    private IIoWarehouseService ioWarehouseService;
    @Resource
    private IWareService iwareService;

    /**
     * 跳转到出入库管理页面
     * @return
     */
    @RequiresPermissions("ioWarehouse")
    @RequestMapping("turnIo")
    public String turnIo(){
        return "warehouse/out_storage_order";
    }

    /**
     * 跳转到未出库交接单页面
     * @return
     */
    @RequiresPermissions("ioWarehouse")
    @RequestMapping("turnHandover")
    public String turnHandover(){
        return "warehouse/handover_storage_order";
    }

    /**
     * 跳转到入库添加页面
     * @return
     */
    @RequiresPermissions("ioWarehouse")
    @RequestMapping("turnIn")
    public String turnIn(String handoverId, String wName, String oNo,String inType, Model model){
        Ware ware = iwareService.getOne(new QueryWrapper<Ware>().eq("ware_name", wName));
        Map<String, Object> pByOrderId = ioWarehouseService.getPByOrderId(oNo);
        model.addAttribute("handoverId",handoverId);
        model.addAttribute("ware",ware);
        model.addAttribute("pByOrderId",pByOrderId);
        model.addAttribute("inType",inType);
        System.out.println(inType);
        return "warehouse/in_storage_order";
    }

    /**
     * 跳转到详情页面
     * @return
     */
    @RequiresPermissions("ioWarehouse")
    @RequestMapping("turnDetail")
    public String turnDetail(String iomNo,Model model){
        model.addAttribute("iomNo",iomNo);
        return "warehouse/detail_storage_order";
    }

    /**
     * 分页列表、模糊查询，出入库列表
     * @param ioNo
     * @param batchNo
     * @param start
     * @param length
     * @return
     */
    @RequiresPermissions("ioWarehouse")
    @ResponseBody
    @PostMapping("page")
    public BlbPage page(String ioNo, String batchNo, int start, int length){
        User user = ShiroUtils.getUser();
        //查询分页数据
        IPage page = ioWarehouseService.page(new Page(start, length), new QueryWrapper<IoWarehouse>()
                .like("io_no", ioNo).like("batch",batchNo).eq("company_id",user.getCompanyId()));
        //查询总条数
        int count = ioWarehouseService.count(new QueryWrapper<IoWarehouse>()
                .like("io_no", ioNo).like("batch",batchNo).eq("company_id",user.getCompanyId()));
        return new DataTablePage(count, count, page.getRecords());
    }

    /**
     * 得到最后的一个批次
     * @return
     */
    @RequiresPermissions("ioWarehouse")
    @ResponseBody
    @RequestMapping("getBatch")
    public ResponseHelp getBatch(){
        ResponseHelp responseHelp = new ResponseHelp();
        responseHelp.setData(ioWarehouseService.getBatch());
        return responseHelp;
    }

    /**
     * 查找所有还没有出库的批次
     * @return
     */
    @RequiresPermissions("ioWarehouse")
    @ResponseBody
    @RequestMapping("getAllBatch")
    public ResponseHelp getAllBatch(){
        ResponseHelp responseHelp = new ResponseHelp();
        User user = ShiroUtils.getUser();
        responseHelp.setData(ioWarehouseService.getAllBatch(user.getCompanyId()));
        return responseHelp;
    }

    /**
     * 通过批次号查找对应的信息
     * @param outBatchNo
     * @return
     */
    @RequiresPermissions("ioWarehouse")
    @ResponseBody
    @RequestMapping("getPNameByBatch")
    public ResponseHelp getPNameByBatch(String outBatchNo){
        ResponseHelp responseHelp = new ResponseHelp();
        responseHelp.setData(ioWarehouseService.getPNameByBatch(outBatchNo));
        return responseHelp;
    }

    /**
     * 通过订单编号查找产品名称以及所对应的所有产品码和总条数
     * @param orderId
     * @return
     */
    @RequiresPermissions("ioWarehouse")
    @ResponseBody
    @PostMapping("getPNameAndCode")
    public BlbPage getPNameAndCode(String orderId){
        List<Map<String, Object>> maps = ioWarehouseService.getPNameAndCode(orderId);
        int countIn = ioWarehouseService.countIn(orderId);
        return new DataTablePage(countIn,countIn,maps);
    }

    /**
     * 入库添加
     * @param iomDetails
     * @param inW
     * @param handoverId
     * @param pNo
     * @param oNo
     * @return
     */
    @RequiresPermissions("ioWarehouse")
    @ResponseBody
    @PostMapping("addIn")
    public ResponseHelp addIn(IomDetailList iomDetails, IoWarehouse inW , String handoverId, String pNo, String oNo){
        User user = ShiroUtils.getUser();
        inW.setCompanyId(user.getCompanyId());
        ioWarehouseService.addInWarehouse(iomDetails,inW,handoverId,pNo,oNo);
        return ResponseHelp.success();
    }

    /**
     * 出库添加
     * @param outW
     * @return
     */
    @RequiresPermissions("ioWarehouse")
    @ResponseBody
    @PostMapping("addOut")
    public ResponseHelp addOut(IoWarehouse outW){
        User user = ShiroUtils.getUser();
        outW.setCompanyId(user.getCompanyId());
        ioWarehouseService.addOutWarehouse(outW);
        return ResponseHelp.success();
    }
}

